package com.lqq.chapter04.HashMapDemo;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author lqq677
 * @Date 2021/8/10 16:25
 * @Description 测试HashMap线程不安全解决方案：ConcurrentHashMap
 */
public class ConcurrentHashMapTest {
    public static void main(String[] args) {
        Map map = new ConcurrentHashMap();
        for (int i = 0; i < 30; i++) {//多个线程同时对集合进行修改
            String key = String.valueOf(i);
            new Thread(() ->{
                //向Map里写数据
                map.put(key, UUID.randomUUID().toString().substring(0, 8));
                //从map中读数据
                System.out.println(map);
            }, key).start();
        }
    }
}
